package com.jiaoyou.baojian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jiaoyou.baojian.BaojianApplication;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.dialog.YesOrNoDialog;
import com.jiaoyou.baojian.utils.GlideCacheUtil;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.modifyPassword)
    TextView modifyPassword;
    @BindView(R.id.clearCache)
    TextView clearCache;
    @BindView(R.id.userLaw)
    TextView userLaw;
    @BindView(R.id.about)
    TextView about;
    @BindView(R.id.quit)
    Button quit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        setTitle("设置");
    }

    @Override
    public int getContentView() {
        return R.layout.activity_setting;
    }

    @OnClick({R.id.modifyPassword, R.id.clearCache, R.id.phoneSetting, R.id.userLaw, R.id.about, R.id.quit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.modifyPassword:
                startActivity(ModifyPasswordActivity.class);
                break;
            case R.id.phoneSetting:
                WebViewActivity.startActivity(this, "file:///android_asset/phone_setting/index.html");
                break;
            case R.id.clearCache:
                String size = "";
                try {
                    size = GlideCacheUtil.getInstance().getCacheSize(this);
                } catch (Exception e) {
                    e.printStackTrace();
                    size = "5M";
                }
                showYesOrNoDialog("缓存大小为" + size + "，确定清除？", new YesOrNoDialog.OnClickListener(){
                    @Override
                    public void onLeftClick() {

                    }

                    @Override
                    public void onRightClick() {
                        //清除自动更新目录
                        File file = new File(getExternalCacheDir().getAbsolutePath() + "/update/");
                        file.delete();
                        GlideCacheUtil.getInstance().clearImageDiskCache(BaojianApplication.getInstance());
                        toast("清除成功");
                    }
                });
                break;
            case R.id.userLaw:
                startActivity(UserLawActivity.class);
                break;
            case R.id.about:
                startActivity(AboutActivity.class);
                break;
            case R.id.quit:
                showYesOrNoDialog("确定要退出帐号吗？", new YesOrNoDialog.OnClickListener(){
                    @Override
                    public void onLeftClick() {

                    }

                    @Override
                    public void onRightClick() {
                        logout();
                        toast("退出成功");
                        Intent intent = new Intent(SettingActivity.this, LoginRegActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                });
                break;
        }
    }
}
